/// vtk 相关文件读取
#include <vtkSmartPointer.h>
#include <vtkPolyDataMapper.h>
#include <vtkStructuredGridReader.h>
#include <vtkStructuredGridGeometryFilter.h>
#include <vtkStructuredPointsReader.h>
#include <vtkImageDataGeometryFilter.h>
#include <vtkRenderWindow.h>
#include <vtkRenderWindowInteractor.h>
#include <vtkRenderer.h>
#include <vtkProperty.h>
#include <vtkStructuredPoints.h>

using namespace std;

int main(int argc, char* argv[])
{
    // Verify input arguments
    if ( argc != 2 )
    {
        std::cout << "Usage: " << argv[0]
                  << " Filename(.vtk)" << std::endl;
        return EXIT_FAILURE;
    }

    std::string inputFilename = argv[1];

    // Read the file
    vtkSmartPointer<vtkStructuredPointsReader> reader = vtkSmartPointer<vtkStructuredPointsReader>::New();
    reader->SetFileName(inputFilename.c_str());
    reader->Update();

    vtkStructuredPoints *stdpoints = reader->GetOutput();
    std::cout << "Scalar Type: " << stdpoints->GetScalarTypeAsString() << std::endl;
    double spacing[3];
    stdpoints->GetSpacing(spacing);
    cout << "spacing: " << spacing[0] << ", " << spacing[1] << ", " << spacing[2] << endl;
    int dims[3];
    stdpoints->GetDimensions(dims);
    cout << "dimensions: " << dims[0] << ", " << dims[1] << ", " << dims[2] << endl;

    int z = 12;
    cout << "data at slice: " << z+1 << endl;
    for (int y = 0; y < dims[1]; y++)
    {
        for (int x = 0; x < dims[0]; x++)
        {
            short* pixel = static_cast<short*>(stdpoints->GetScalarPointer(x,y,z));
            cout << setw(8) << pixel[0];
        }
        cout << endl;
    }
    cout << "============" << endl;

//  vtkSmartPointer<vtkStructuredGridGeometryFilter> geometryFilter =
//    vtkSmartPointer<vtkStructuredGridGeometryFilter>::New();
//  geometryFilter->SetInputConnection(reader->GetOutputPort());
//  geometryFilter->Update();

    vtkSmartPointer<vtkImageDataGeometryFilter> geometryFilter =
            vtkSmartPointer<vtkImageDataGeometryFilter>::New();
    geometryFilter->SetInputConnection(reader->GetOutputPort());
    geometryFilter->Update();

    // Visualize
    vtkSmartPointer<vtkPolyDataMapper> mapper =
            vtkSmartPointer<vtkPolyDataMapper>::New();
    mapper->SetInputConnection(geometryFilter->GetOutputPort());

    vtkSmartPointer<vtkActor> actor =
            vtkSmartPointer<vtkActor>::New();
    actor->SetMapper(mapper);
    actor->GetProperty()->SetOpacity(0.5);

    vtkSmartPointer<vtkRenderer> renderer =
            vtkSmartPointer<vtkRenderer>::New();
    vtkSmartPointer<vtkRenderWindow> renderWindow =
            vtkSmartPointer<vtkRenderWindow>::New();
    renderWindow->SetSize(800, 600);
    renderWindow->AddRenderer(renderer);
    vtkSmartPointer<vtkRenderWindowInteractor> renderWindowInteractor =
            vtkSmartPointer<vtkRenderWindowInteractor>::New();
    renderWindowInteractor->SetRenderWindow(renderWindow);

    renderer->AddActor(actor);
    renderer->SetBackground(.1, .1, .1);

    renderWindow->Render();
    renderWindowInteractor->Start();

    return EXIT_SUCCESS;
}
